package com.apes.fn.scm.retailOrder.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.Arith;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Product;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * @program: apes-server
 * @description
 * @author: czs
 * @create: 2022-01-05 20:05:01
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PlatformFlow.CONST_TABLE_NAME)
public class PlatformFlow extends Model {
    public static final String CONST_TABLE_NAME = "platform_flow";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "平台引流明细编码", padding = 8)
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "platform_id", referencedColumnName = "id")
    @Comment("平台")
    private PartyRole platform;

    @Column(name = "order_id", length = 16)
    @Comment("单据号码")
    private String orderId;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "statistic_date")
    @Comment("统计日期")
    private Date statisticDate;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "order_creator_date")
    @Comment("单据创建日期")
    private Date orderCreatorDate;

    @Comment("部门")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    private Dept dept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    @Comment("公司")
    private PartyRole company;

    @Column(name = "car_license", length = 64)
    @Comment("车牌号码")
    private String carLicense;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("商品")
    private Product product;

    @Column(name = "sales_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("销售金额")
    private double salesAmount = 0;

    @Column(name = "discount_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("折扣金额")
    private double discountAmount = 0;

    @Column(name = "pay_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("线下支付金额")
    private double payAmount = 0; //线下支付金额

    //offlineCompensationFlow 线下支付引流费 onlinePayFlow 线上支付引流费用 platformSubsidy 平台券补贴
    @Column(name = "flow_type", length = 64)
    @Comment("引流方式")
    private String flowType;

    @Column(name = "platform_coupon_id", length = 64)
    @Comment("平台券ID")
    private String platformCouponId;

    @Column(name = "platform_coupon_discount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("平台券优惠价")
    private double platformCouponDiscount = 0;

    public void setOnlinePayFlow(double onlinePayFlow) {
        this.onlinePayFlow = Arith.round(onlinePayFlow,2);
    }

    @Column(name = "online_pay_flow", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("线上支付引流费")
    private double onlinePayFlow = 0;

    public void setOfflineCompensationFlow(double offlineCompensationFlow) {
        this.offlineCompensationFlow = Arith.round(offlineCompensationFlow,2);
    }

    @Column(name = "offline_compensation_flow", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("线下补差引流费")
    private double offlineCompensationFlow = 0;

    @Column(name = "platform_coupon_subsidy", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("平台券补贴")
    private double platformCouponSubsidy = 0;

}
